package cim3d;

import javax.media.opengl.GL;
import java.util.Timer;
import java.util.List;
import java.util.TimerTask;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2009-1-15
 */
public abstract class DynamicRenderer extends TimerTask {

    private Timer timer = new Timer();
    int state = VIRGIN;

    static final int VIRGIN = 0;
    static final int SCHEDULED = 1;

    private long period = 500;

    protected int[] showList;
    private boolean isDataUpdated;
    private boolean isHide=false;


    public boolean isDataUpdated() {
        return isDataUpdated;
    }

    public void setDataUpdated(boolean dataUpdated) {
        isDataUpdated = dataUpdated;
    }

    public int[] getShowList() {
        return showList;
    }

    protected GL gl;

    protected List elements;

    protected Integer currentListIndex = -1;

    protected DynamicRenderer() {
    }

    protected DynamicRenderer(GL gl) {
        this.gl = gl;
    }

    protected DynamicRenderer(GL gl, List elements) {
        this.gl = gl;
        this.elements = elements;
        start();
    }

    public void start() {
        if (state == VIRGIN) {
            timer.schedule(this, 0, period);
            state = SCHEDULED;
        }
    }

    public void stop() {
        timer.cancel();
    }

    public abstract void createShowList();

    public void showCurrentList() {
        synchronized (currentListIndex){
            if (currentListIndex != -1 && gl != null && currentListIndex<showList.length)
                gl.glCallList(showList[currentListIndex]);
        }
    }

    public void run() {
        synchronized (currentListIndex){
            if (currentListIndex == -1)
                return;
            currentListIndex++;
            if (currentListIndex >= showList.length)
                currentListIndex = 0;
        }
    }

    public long getPeriod() {
        return period;
    }

    public void setPeriod(long period) {
        this.period = period;
    }

    public GL getGl() {
        return gl;
    }

    public void setGl(GL gl) {
        this.gl = gl;
    }

    public List getElements() {
        return elements;
    }

    public void setElements(List elements) {
        this.elements = elements;
    }

    public int getCurrentListIndex() {
        return currentListIndex;
    }

    public boolean isHide() {
        return isHide;
    }

    public void setHide(boolean hide) {
        isHide = hide;
    }
}
